using System;
using System.Web.UI;
using Churchtoolkit.Utils.MVP;
using Microsoft.Practices.ServiceLocation;

namespace Churchtoolkit.Utils.ErrorProvider
{
    public class Page<TPresenter> : MVP.Page<TPresenter> where TPresenter : Presenter
    {
       public ValidationPlaceHolderPairCollection Collection { get; set; }
        public Page()
        {
            Collection = new ValidationPlaceHolderPairCollection();
            ErrorProvider = ServiceLocator.Current.GetInstance<IErrorProvider>();
        }

        protected IErrorProvider ErrorProvider { private set; get; }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (!IsPostBack)
            {
                foreach (IValidator validator in Validators)
                {
                    validator.ErrorMessage = ErrorProvider.RetriveErrorMessage(validator.ErrorMessage);
                }
                this.Collection.ReplaceAllPlaceHolders();
            }
        }

    }
}
